# Remove path separator from WORDCHARS.
WORDCHARS=${WORDCHARS//[\/]}

# -----------------
# Zim configuration
# -----------------

if [[ ! -e ${ZIM_HOME}/zimfw.zsh ]]; then
  # Download zimfw script if missing.
  command mkdir -p ${ZIM_HOME}
  command curl -fsSL -o ${ZIM_HOME}/zimfw.zsh https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
fi

if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZDOTDIR:-${HOME}}/.zimrc ]]; then
  # Update static initialization script if it does not exist or it's outdated, before sourcing it
  source ${ZIM_HOME}/zimfw.zsh init -q
fi

source ${ZIM_HOME}/init.zsh

# ------------------------------
# Post-init module configuration
# ------------------------------

bindkey -e

# zsh-users/zsh-history-substring-search
bindkey '^P' history-substring-search-up
bindkey '^N' history-substring-search-down

# bindkey '^[' zsh_gh_copilot_explain  # bind Option+shift+\ to explain
# bindkey '^]' zsh_gh_copilot_suggest  # bind Option+\ to suggest

export HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=8,bold,fg=white'
export HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=8,fg=red'

function have_cmd {
    if ! hash $1 2>/dev/null; then
        # echo "zshrc: Command '$1' is not installed"
        return 1
    fi
}

# lewis6991/fancy-prompt
export FANCY_PROMPT_DOUBLE_LINE=1
export FANCY_PROMPT_USE_NERD_SYMBOLS=1

# zsh-users/zsh-autosuggestions
export ZSH_AUTOSUGGEST_USE_ASYNC=1

# History ----------------------------------------------------------------------
if [ -d "$XDG_DATA_HOME" ]; then
    HISTFILE="$XDG_DATA_HOME/zsh_history"
fi
HISTSIZE=50000
SAVEHIST=10000
setopt HIST_IGNORE_ALL_DUPS
setopt SHARE_HISTORY
setopt HIST_REDUCE_BLANKS

# Completion -------------------------------------------------------------------
zmodload zsh/complist

# Enable shift-tab to cycle back completions
bindkey -M menuselect '^[[Z' reverse-menu-complete
setopt menu_complete

! [ -f ~/.aliases        ] || source ~/.aliases
! [ -f ~/.bash_functions ] || source ~/.bash_functions
! [ -f ~/.aliases_local  ] || source ~/.aliases_local
! [ -f ~/.zshrc_local    ] || source ~/.zshrc_local

# Pager ------------------------------------------------------------------------
export PAGER="less"

export LESS="\
    --RAW-CONTROL-CHARS \
    --ignore-case \
    --LONG-PROMPT \
    --quit-if-one-screen \
    --chop-long-lines"

export MANPAGER='nvim +Man!'

# Other ----------------------------------------------------------------------

# setopt NO_BEEP

# Stop ctrl-d from closing the shell
setopt IGNORE_EOF

# Aliases ----------------------------------------------------------------------

# Give command history to tclsh
if have_cmd rlwrap; then
    if have_cmd tclsh; then
        alias tclsh="rlwrap -Ar -pcyan tclsh"
    fi
    if have_cmd lua; then
        alias lua="rlwrap -Ar -pcyan --always-readline lua"
    fi
    if have_cmd luajit; then
        alias luajit="rlwrap -Ar -pcyan --always-readline luajit"
    fi
fi

rg() {
    if [[ -t 1 ]]; then
        if ! hash delta 2>/dev/null; then
            command rg --json --smart-case "$@" | delta
        else
            command rg --pretty --smart-case "$@" | less
        fi
    else
        command rg "$@"
    fi
}

# --RAW-CONTROL-CHARS cause --quit-if-one-screen to not work
export LESS="\
    --raw-control-chars \
    --ignore-case \
    --LONG-PROMPT \
    --quit-if-one-screen \
    --chop-long-lines"

ulimit -c unlimited

java() {
    unfunction java
    if ! hash jenv 2>/dev/null; then
        eval "$(jenv init -)"
    fi
    command java "$@"
}

unfunction have_cmd
